import { Component } from '@angular/core';
import { NavController, NavParams, ViewController, LoadingController, ToastController } from 'ionic-angular';
import { BaseUI } from '../common/baseui';
import { Storage } from '@ionic/storage';
import { RestProvider } from '../../providers/rest/rest';
import { RegisterPage } from '../register/register';
// import { Md5  } from 'ts-md5/dist/md5';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

// @IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage extends BaseUI{

  mobile: any;
  password: any;
  errMesg : any;

  constructor(public navCtrl: NavController
              , public navParams: NavParams
              , public viewCtrl: ViewController
              , public rp: RestProvider
              , public loadCtrl:LoadingController
              , public storage: Storage
              , public toastCtrl:ToastController) {
    // 继承了BaseUi， 后需要构造函数里面调用一个父类的构造器
    super();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  /**
   *登录操作
   *
   * @memberof LoginPage
   */
  goLogin() {
    console.log(this.mobile + '  ' + this.password);
    // 显示一个菊花， 调用父类的方法
    var loading = super.showLoading(this.loadCtrl, '登录中....');
    this.rp.login(this.mobile, this.password)
          .subscribe( 
            f => {
              if (f["Status"] == "OK") {
                //处理登录成功的页面跳转
                //你也可以存储接口返回的 token 在本地存储用户信息
                this.storage.set('UserId',f["UserId"]);
                loading.dismiss();// 关闭loading的菊花
                this.dismiss();// 关闭登录页面； 
              }
              else {
                loading.dismiss();// 关闭loading的菊花
                super.showToad(this.toastCtrl, f["StatusContent"]); // 提示登录错误信息； 
              }
            },  
            error => this.errMesg = <any>error);

  }

  /**
   * 关闭显示视图
   *
   * @memberof LoginPage
   */
  dismiss () {
      this.viewCtrl.dismiss();
  }

  register () {
    this.navCtrl.push(RegisterPage);
  }
}
